package com.smasher.bluetooth.scan;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import androidx.annotation.RequiresPermission;
import androidx.core.content.ContextCompat;

public class DiscoveringClassic extends BaseDiscovering {

    public static final String TAG = "ClassicDiscovering";

    public DiscoveringClassic(Context context) {
        super(context);
    }

    @Override
    public void init(Context context) {
        registerBluetoothReceiver(context);
    }

    @RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
    @Override
    public void startDiscovering() {
        //扫描 大概12s,扫描中不能立即重新扫描
        if (bluetoothAdapter == null) {//取消扫描
            return;
        }

        if (bluetoothAdapter.isDiscovering()) {
            //取消当前正在搜索设备...
            bluetoothAdapter.cancelDiscovery();
        }

        if (!bluetoothAdapter.isDiscovering()) {
            boolean result = bluetoothAdapter.startDiscovery();
            Log.i(TAG, "startDiscovering: " + result);
        }
    }

    @RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
    @Override
    public void stopDiscovering() {
        if (bluetoothAdapter == null) {//取消扫描
            return;
        }

        if (bluetoothAdapter.isDiscovering()) {
            //取消当前正在搜索设备...
            bluetoothAdapter.cancelDiscovery();
        }
    }

    @RequiresPermission(Manifest.permission.BLUETOOTH_SCAN)
    @Override
    public void release(Context context) {
        super.release(context);
        if (bluetoothAdapter == null) {//取消扫描
            return;
        }

        if (bluetoothAdapter.isDiscovering()) {
            //取消当前正在搜索设备...
            bluetoothAdapter.cancelDiscovery();
        }
        unregisterBluetoothReceiver(context);
    }


    private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e(TAG, "onReceive: intent -");
            String action = intent.getAction();
            Log.e(TAG, "onReceive: " + action);
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {//扫描-发现设备
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device != null) {
                    Log.e(TAG, "onReceive: 发现设备 -");
                    if (mDeviceFoundListener != null) {
                        mDeviceFoundListener.onDeviceFound(device);
                    }
                }
            }

            if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {//扫描结束
                Log.e(TAG, "onReceive: 扫描结束");
            }
            if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {//扫描开始
                Log.e(TAG, "onReceive: 扫描开始");
            }

        }
    };

    /**
     * 声明为 RECEIVER_EXPORTED
     * sendBroadcast 必须为 Intent 指定 package
     * 为了兼容未指定 package 的广播 保证能接收到 所以使用 RECEIVER_EXPORTED
     */
    public void registerBluetoothReceiver(Context context) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        ContextCompat.registerReceiver(context, bluetoothReceiver, intentFilter, ContextCompat.RECEIVER_EXPORTED);

    }

    private void unregisterBluetoothReceiver(Context context) {
        context.unregisterReceiver(bluetoothReceiver);
    }
}
